<?php
/**
 * Created by PhpStorm.
 * User: hongyeah
 * Date: 2017/9/16
 * Time: 13:34
 */

namespace app\index\controller;

use think\Controller;
use think\Request;
use \think\Session;
use \think\Cookie;
use \think\Cache;

define('PROJECT_OK_STATUS', 1);
define('LOGIN_KEY', '&*HFDjfdslj9872@@');

class Project extends Controller
{

    public function projectAct()
    {
        $request = Request::instance();
        $id = $request->get('id');
        $id = intval($id);

        if ($id > 0) {
            $projectModel = model('Project');
            $fields = "id, activity";
            $result = $projectModel->field($fields)->where('id', $id)->where('status', PROJECT_OK_STATUS)->find();
            if (!empty($result)) {
                format(CODE_OK, '', $result['activity']);
            } else {
                format(CODE_SYSTEM_FAIL, '系统异常, 请稍后再试');
            }
        } else {
            format(CODE_PARAM_FAIL, '系统异常, 请稍后再试');
        }
    }

    //项目列表
    public function projectList()
    {
        $request = Request::instance();
        $page = $request->get('page');
        $pageSize = $request->get('ps');

        $page = intval($page);
        $pageSize = intval($pageSize);

        if (empty($page) || $page < 0) {
            $page = 1;
        }

        if (empty($pageSize) || $pageSize < 0) {
            $pageSize = 10;
        }

        $order = array(
            'order' => 'desc',
            'update_time' => 'desc',
            'id' => 'desc'
        );

        $projectModel = model('Project');

        $ret['projectList'] = $projectModel->field("id, title, cover_img")->where('status', PROJECT_OK_STATUS)->order($order)->page($page, $pageSize)->select();
        $ret['amount'] = $projectModel->count();
        $ret['page'] = $page;
        $ret['pageSize'] = $pageSize;

        if (!empty($ret['projectList'])) {
            format(CODE_OK, '', $ret);
        } else {
            format(CODE_SYSTEM_FAIL, '系统异常, 请稍后再试');
        }
    }

    //项目介绍
    public function projectIntroduce()
    {
        $request = Request::instance();
        $id = $request->get('id');
        $id = intval($id);

        if ($id > 0) {
            $projectModel = model('Project');
            $fields = "id, title, cover_img, house_type, open_time, sale_type, phone, activity";
            $result = $projectModel->field($fields)->where('id', $id)->where('status', PROJECT_OK_STATUS)->find();
            if (!empty($result)) {
                format(CODE_OK, '', $result);
            } else {
                format(CODE_SYSTEM_FAIL, '系统异常, 请稍后再试');
            }
        } else {
            format(CODE_PARAM_FAIL, '系统异常, 请稍后再试');
        }
    }

    //项目详情
    public function projectDetail()
    {
        $request = Request::instance();
        $id = $request->get('id');
        $id = intval($id);

        if ($id > 0) {
            $projectModel = model('Project');
            $fields = "id, feature, introduce, config, traffic, material, floor";
            $result = $projectModel->field($fields)->where('id', $id)->where('status', PROJECT_OK_STATUS)->find();

            if (!empty($result)) {
                format(CODE_OK, '', $result);
            } else {
                format(CODE_SYSTEM_FAIL, '系统异常, 请稍后再试');
            }
        } else {
            format(CODE_PARAM_FAIL, '系统异常, 请稍后再试');
        }
    }
    public function projectImg()
    {
        $request = Request::instance();
        $id = $request->get('id');
        $id = intval($id);

        if ($id > 0) {
            $projectModel = model('Project');
            $fields = "id, ext_img, model_img, real_img";
            $result = $projectModel->field($fields)->where('id', $id)->find();
            $result['ext_img'] = json_decode($result['ext_img']);
            if (!empty($result['ext_img'])) {
                $result['ext_img'] = $result['ext_img'][0];
            } else {
                $result['ext_img'] = '';
            }

            $result['model_img'] = json_decode($result['model_img']);
            if (!empty($result['model_img'])) {
                $result['model_img'] = $result['model_img'][0];
            } else {
                $result['model_img'] = '';
            }

            $result['real_img'] = json_decode($result['real_img']);
            if (!empty($result['real_img'])) {
                $result['real_img'] = $result['real_img'][0];
            } else {
                $result['real_img'] = '';
            }


            format(CODE_OK, '', $result);

        } else {
            format(CODE_PARAM_FAIL);
        }
    }
    //项目相册
    public function projectImgDetail()
    {
        $request = Request::instance();
        $id = $request->get('id');
        $type = $request->get('type');

        $id = intval($id);

        $typeMap = array(
            'ext_img', 'model_img', 'real_img'
        );

        if ($id > 0 && in_array($type, $typeMap)) {
            $projectModel = model('Project');
            $fields = "id, {$type}";
            $result = $projectModel->field($fields)->where('id', $id)->where('status', PROJECT_OK_STATUS)->find();
            $result[$type] = json_decode($result[$type], true);

            if (!empty($result[$type])) {
                $result = $result[$type];
            } else {
                $result = array();
            }


            format(CODE_OK, '', $result);

        } else {
            format(CODE_OK, '', array());
        }
    }

    public function introducePage()
    {
        //Session::set('id', 1);
        //$this->checkLogin();
        $request = Request::instance();
        $id = $request->request('id');
        $id = intval($id);

        $houseAreaModel = model("HouseArea");
        $houseTypeModel = model('HouseType');
        $projectModel = model('Project');
        $saleAdvisorModel = model('SaleAdvisor');

        $houseArea = $houseAreaModel->field('name')->where('status', 1)->order("order", "desc")->select();
        $houseType = $houseTypeModel->field('name')->where('status', 1)->order("order", "desc")->select();
        $houseList = $projectModel->field('title')->where('status', 1)->order("order", "desc")->select();
        $sales = $saleAdvisorModel->field('id, name')->where('status', 1)->select();

        $title = '';

        if ($id >= 0 ) {
            $project = $projectModel->field('title')->where('id', $id)->find();
            $title = !empty($project['title']) ? $project['title'] : '';
        }

        format(CODE_OK, '', array(
            'area_list'=>$houseArea,
            'type_list'=>$houseType,
            'house'=>$title,
            'house_list'=>$houseList,
            'sale_list'=>$sales
        ));
    }

    public function introduce()
    {
        //Session::set('id', 1);
        $this->checkLogin();
        $request = Request::instance();
        $introducerId = Session::get('id');
        $name = $request->request('name', '', 'SAFE_REPLACE,REMOVE_XSS');
        $phone = $request->request('phone');
        $area = $request->request('area', '', 'SAFE_REPLACE,REMOVE_XSS');
        $type = $request->request('type', '', 'SAFE_REPLACE,REMOVE_XSS');
        $house = $request->request('house', '', 'SAFE_REPLACE,REMOVE_XSS');
        $sale = $request->request('sale', '', 'SAFE_REPLACE,REMOVE_XSS');
        $sale = intval($sale);

        if (!isPhone($phone) || empty($name) || empty($area) || empty($type) || empty($house) || empty($sale) || empty($introducerId)) {
            format(CODE_PARAM_FAIL, '系统异常, 请稍后再试');
        }

        $staffModel = model('Staff');
        $customerModel = model('Customer');
        $introducerModel = model('Introducer');
        $isStaff = $staffModel->where('status', 1)->where('phone', $phone)->find();
        $isCustomer = $customerModel->where('status', 1)->where('phone', $phone)->find();
        $isIntroducer = $introducerModel->where('status', 1)->where('phone', $phone)->find();
        $advisor = model('SaleAdvisor')->where('id', $sale)->find();
        $vip = model('Vip')->where('introduce_status', '<>', '失效')->where('phone', $phone)->find();

        if (!empty($isStaff)) {
            format(CODE_OPT_FAIL, '员工不能被推介');
        }
        if (!empty($isCustomer)) {
            format(CODE_OPT_FAIL, '客户已到访过啦');
        }
        if (!empty($vip)) {
            format(CODE_OPT_FAIL, '客户已被推介');
        }
        if (!empty($isIntroducer)) {
            format(CODE_OPT_FAIL, '对方也是推介人');
        }

        if (empty($isStaff) && empty($isCustomer) && empty($vip) && empty($isIntroducer)) {
            $data = array(
                'introducer_id' => $introducerId,
                'name' => $name,
                'phone' => $phone,
                'house' => $house,
                'house_type' => $type,
                'house_area' => $area,
                'advisor_id' => $sale,
                'advisor_name' => !empty($advisor['name']) ? $advisor['name'] : '',
                'introduce_status' => '推介',
                'create_time' => time(),
                'update_time' => time()
            );
            $result = model('Vip')->data($data)->save();
            $vipId = model('Vip')->getLastInsID();
            $introduceStatusLogModel = model('IntroduceStatusLog');
            $statusLogData = array(
                'introduce_id'=> $vipId,
                'status' => '推介',
                'create_time'=>time()
            );
            $introduceStatusLogModel->data($statusLogData)->save();
            if ($result !== false) {
                format(CODE_OK, '推介成功');
            } else {
                format(CODE_SYSTEM_FAIL, '系统异常, 请稍后再试');
            }
        } else {
            format(CODE_OPT_FAIL, '该客户已被推介');
        }

    }

    public function projectAddress()
    {
        $request = Request::instance();
        $id = $request->get('id');
        $id = intval($id);

        if ($id > 0) {
            $projectModel = model('Project');
            $fields = "id, address, lng, lat";
            $result = $projectModel->field($fields)->where('id', $id)->find();

            format(CODE_OK, '', $result);

        } else {
            format(CODE_PARAM_FAIL);
        }
    }

}